import type { MockMethod } from "vite-plugin-mock";
import Mock from "mockjs";

// 模拟库存列表数据
const stockList = Mock.mock({
  "list|10-20": [
    {
      "id|+1": 1,
      product_sn: "@string(5, 10)",
      product_name: "@ctitle(5, 10)",
      sku_code: "@string(10, 15)",
      attributes: {
        color: "@color",
        size: "@string(2, 4)"
      },
      "stock|0-100": 1,
      "price|100-1000": 1,
      image: "@image('200x100', '#50B347', '#FFF', 'Mock Image')",
      "is_available|0-1": 1,
      changed_at: "@datetime"
    }
  ],
  total: 50 // 模拟总条数
});

// 模拟接口
export default [
  {
    url: "/api/stock/list", // 查询库存列表
    method: "get",
    response: ({ query }) => {
      const { pageNum = 1, pageSize = 10 } = query;
      const start = (pageNum - 1) * pageSize;
      const end = start + Number(pageSize);
      return {
        code: 200,
        data: {
          list: stockList.list.slice(start, end),
          total: stockList.total
        }
      };
    }
  },
  {
    url: "/api/stock", // 新增库存
    method: "post",
    response: ({ body }) => {
      return {
        code: 200,
        data: {
          id: Mock.mock("@integer(100, 1000)"),
          ...body
        }
      };
    }
  },
  {
    url: "/api/stock", // 更新库存
    method: "put",
    response: ({ body }) => {
      return {
        code: 200,
        data: body
      };
    }
  },
  {
    url: "/api/stock/:id", // 删除库存
    method: "delete",
    response: ({ query }) => {
      return {
        code: 200,
        data: query.id
      };
    }
  },
  {
    url: "/api/stock/batch", // 批量删除库存
    method: "delete",
    response: ({ body }) => {
      return {
        code: 200,
        data: body.ids
      };
    }
  },
  {
    url: "/api/stock/status", // 更新库存状态
    method: "put",
    response: ({ body }) => {
      return {
        code: 200,
        data: body
      };
    }
  }
] as MockMethod[];
